package CCRMClient.GUI;
/**
 * This screen is available only to the marketing manager.
 * It's including reports and sale initiations.
 * @author Oz Mouchtar 
 */
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.ClientSaleManager;
import Common.SERVER_COMMANDS;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.awt.Color;

import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

public class MarketingManagementScreen extends JPanel {
	private JLabel lblMarketingManagement;
	private JButton btnSaleInitiation;
	private JButton btnReports;
	private JButton btnGoBack;
	private JButton btnCatalog;
	private JButton btnTemplates;
	private JButton btnAddRegion;
	public MarketingManagementScreen() {
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBackground(Color.WHITE);
		initComponents();
	}
	private void initComponents() {
		setLayout(null);
		setSize(490, 390);
		lblMarketingManagement = new JLabel("Marketing Management");
		lblMarketingManagement.setForeground(Color.RED);
		lblMarketingManagement.setHorizontalAlignment(SwingConstants.CENTER);
		lblMarketingManagement.setFont(new Font("David", Font.BOLD, 26));
		lblMarketingManagement.setBounds(0, 11, 466, 27);
		add(lblMarketingManagement);
		
		btnSaleInitiation = new JButton("Sale Initiation");
		btnSaleInitiation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.GET_APPROVED_TEMPLATES, ClientSaleManager.getApprovedTemplates(SERVER_COMMANDS.GET_APPROVED_TEMPLATES));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			}
		});
		btnSaleInitiation.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnSaleInitiation.setBounds(10, 56, 179, 23);
		add(btnSaleInitiation);
		
		
		btnReports = new JButton("Reports");
		btnReports.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if ((!ClientStart.user.getPosition().equals("Marketing Manager")) )
					JOptionPane.showMessageDialog(null, "You are not authorized to do this action!", "Security Issue", JOptionPane.ERROR_MESSAGE);
				else 
				ClientStart.panelSwicher(new ReportsScreen());
			}
		});
	
		btnReports.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnReports.setBounds(10, 90, 179, 23);
		add(btnReports);
		
		btnGoBack = new JButton("Go Back");
		btnGoBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ClientStart.panelSwicher(new MarketingScreen());
			}
		});
		btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnGoBack.setBounds(10, 315, 124, 23);
		add(btnGoBack);
		add(getBtnCatalog());
		add(getBtnTemplates());
		add( getBtnAddRegion());
	}
	JButton getBtnAddRegion() {
		if (btnAddRegion == null) {
			btnAddRegion = new JButton("Add region");
			btnAddRegion.addActionListener(new BtnAddRegionActionListener());
			btnAddRegion.setFont(new Font("Tahoma", Font.BOLD, 16));
			btnAddRegion.setBounds(282, 127, 179, 23);
		}
		return btnAddRegion;
	}
	
	JButton getBtnCatalog() {
		if (btnCatalog == null) {
			btnCatalog = new JButton("Catalog");
			btnCatalog.addActionListener(new BtnCatalogActionListener());
			btnCatalog.setFont(new Font("Tahoma", Font.BOLD, 16));
			btnCatalog.setBounds(282, 56, 179, 23);
		}
		return btnCatalog;
	}
	
JButton getBtnTemplates() {
	if (btnTemplates == null) {
		btnTemplates = new JButton("Templates");
		btnTemplates.addActionListener(new BtnTemplatesActionListener());
		btnTemplates.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnTemplates.setBounds(282, 90, 179, 23);
	}
	return btnTemplates;
}

	/**
	 * This inner class implements event listener for pressing Catalog button
	 * @author Ori Ziv
	 *
	 */
	private class BtnCatalogActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new CatalogScreen());
		}
	}
	
	/**
	 * This inner class implements event listener for pressing Templates button
	 * @author AL
	 *
	 */
	private class BtnTemplatesActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new TemplateMarketingManagerScreen());
		}
	}
	/**
	 * This inner class implements event listener for pressing AddRegion button
	 * @author Ola
	 *
	 */
	private class BtnAddRegionActionListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0){
			if ((!ClientStart.user.getPosition().equals("Marketing Manager")) )
				JOptionPane.showMessageDialog(null, "You are not authorized to do this action!", "Security Issue", JOptionPane.ERROR_MESSAGE);
			else {
			ClientStart.panelSwicher(new AddRegionScreen());
		}
	}
}
	private class BtnReportsActionListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0){
			if ((!ClientStart.user.getPosition().equals("Marketing Manager")) )
				JOptionPane.showMessageDialog(null, "You are not authorized to do this action!", "Security Issue", JOptionPane.ERROR_MESSAGE);
			else {
			ClientStart.panelSwicher(new ReportsScreen());
		}
		}
	}
}
